﻿#include "pagehelper.h"
#include "ui_pagehelper.h"

PageHelper::PageHelper(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageHelper)
{
    ui->setupUi(this);
    itemTotal=0;
    resetContol();
    pageCount = ui->pageCount->currentText().toInt();
    ui->pageCount->setValidator(new QIntValidator(1,10000,this));
}

PageHelper::~PageHelper()
{
    delete ui;
}

void PageHelper::setItemTotal(const qlonglong &value)
{
    itemTotal = value;
    nowPage = 1;
    resetContol();
}



void PageHelper::resetContol()
{
    //总条目显示
    ui->itemTotal->setText(QString("共%1条").arg(itemTotal));
    //计算总共页码数
    pageTotal=itemTotal/ui->pageCount->currentText().toInt();
    int pagea=itemTotal%ui->pageCount->currentText().toInt();

    if(pagea>0){
        pageTotal++;
    }

    lastPage = nowPage - 1;
    beginPage = 1;
    nextPage = nowPage + 1;
    endPage = pageTotal;

    ui->gotoNum->setMinimum(beginPage);
    ui->gotoNum->setMaximum(endPage);
    ui->gotoNum->setValue(nowPage);

    ui->beginPage->setText(QString("首页"));
    ui->lastPage->setText(QString::number(lastPage));
    ui->nowPage->setText(QString::number(nowPage));
    ui->nextPage->setText(QString::number(nextPage));
    ui->endPage->setText(QString("尾页"));


    ui->lastPageBtn->setVisible(pageTotal > 0 && nowPage - beginPage >= 1);
    ui->beginPage->setVisible(pageTotal > 0 && nowPage - beginPage >= 2);
    ui->lastOmit->setVisible(pageTotal > 0 && nowPage - beginPage >= 2);
    ui->lastPage->setVisible(pageTotal > 0 && lastPage >= beginPage);
    ui->nowPage->setVisible(pageTotal > 0);
    ui->nextPage->setVisible(pageTotal > 0 && nextPage <= endPage);
    ui->nextOmit->setVisible(pageTotal > 0 && endPage - nowPage >=2);
    ui->endPage->setVisible(pageTotal > 0 && endPage - nowPage >=2);
    ui->nextPageBtn->setVisible(pageTotal > 0 && endPage - nowPage >=1);

    ui->gotoLabel->setVisible(pageTotal > 0);
    ui->gotoNum->setVisible(pageTotal > 0);
    ui->gotoBtn->setVisible(pageTotal > 0);

    ui->pageCountLabel->setVisible(pageTotal > 0);
    ui->pageCount->setVisible(pageTotal > 0);

}


void PageHelper::on_beginPage_clicked()
{
    nowPage = beginPage;
    pageChange();
}

void PageHelper::on_endPage_clicked()
{
    nowPage = endPage;
    pageChange();
}

void PageHelper::on_nextPageBtn_clicked()
{
    nowPage +=1;
    pageChange();
}

void PageHelper::on_lastPageBtn_clicked()
{
    nowPage --;
    pageChange();
}

void PageHelper::on_gotoBtn_clicked()
{
    nowPage = ui->gotoNum->text().toInt();
    pageChange();
}

void PageHelper::on_lastPage_clicked()
{
    on_lastPageBtn_clicked();
}

void PageHelper::on_nextPage_clicked()
{
    on_nextPageBtn_clicked();
}

void PageHelper::on_pageCount_activated(const QString &arg1)
{
    pageCount = ui->pageCount->currentText().toInt();
    nowPage=beginPage;
    pageChange();
}

void PageHelper::setPageCount(int value)
{
    pageCount = value;
    ui->pageCount->setEditText(QString::number(pageCount));
}

qlonglong PageHelper::getPageTotal() const
{
    return pageTotal;
}

qlonglong PageHelper::getNowPage() const
{
    return nowPage;
}

void PageHelper::pageChange()
{
    resetContol();
    emit signal_goto(nowPage,pageCount);
}
